{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 参考模块源码\n",
    "\n",
    "- `psutil._cpmmon`\n",
    "- `psutil._compat`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 判断解释器版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "py36 = sys.version_info >= (3, 6)\n",
    "py33 = sys.version_info >= (3, 3)\n",
    "py35 = sys.version_info >= (3, 5)\n",
    "py32 = sys.version_info >= (3, 2)\n",
    "py3k = sys.version_info >= (3, 0)\n",
    "py2k = sys.version_info < (3, 0)\n",
    "py265 = sys.version_info >= (2, 6, 5)\n",
    "jython = sys.platform.startswith(\"java\")\n",
    "pypy = hasattr(sys, \"pypy_version_info\")\n",
    "\n",
    "win32 = sys.platform.startswith(\"win\")\n",
    "cpython = not pypy and not jython  # TODO: something better for this ?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "\"\"\"\n",
    "兼容Python版本\n",
    "\"\"\"\n",
    "\n",
    "import sys\n",
    "\n",
    "is_py2 = (sys.version_info[0] == 2)\n",
    "is_py3 = (sys.version_info[0] == 3)\n",
    "is_py33 = (sys.version_info[0] == 3 and sys.version_info[1] == 3)\n",
    "\n",
    "\n",
    "try:\n",
    "    import simplejson as json\n",
    "except (ImportError, SyntaxError):\n",
    "    import json\n",
    "\n",
    "\n",
    "if is_py2:\n",
    "    from urllib import quote as urlquote, unquote as urlunquote\n",
    "    from urlparse import urlparse\n",
    "\n",
    "\n",
    "    def to_bytes(data):\n",
    "        \"\"\"若输入为unicode， 则转为utf-8编码的bytes；其他则原样返回。\"\"\"\n",
    "        if isinstance(data, unicode):\n",
    "            return data.encode('utf-8')\n",
    "        else:\n",
    "            return data\n",
    "\n",
    "    def to_string(data):\n",
    "        \"\"\"把输入转换为str对象\"\"\"\n",
    "        return to_bytes(data)\n",
    "\n",
    "    def to_unicode(data):\n",
    "        \"\"\"把输入转换为unicode，要求输入是unicode或者utf-8编码的bytes。\"\"\"\n",
    "        if isinstance(data, bytes):\n",
    "            return data.decode('utf-8')\n",
    "        else:\n",
    "            return data\n",
    "\n",
    "    def stringify(input):\n",
    "        if isinstance(input, dict):\n",
    "            return dict([(stringify(key), stringify(value)) for key,value in input.iteritems()])\n",
    "        elif isinstance(input, list):\n",
    "            return [stringify(element) for element in input]\n",
    "        elif isinstance(input, unicode):\n",
    "            return input.encode('utf-8')\n",
    "        else:\n",
    "            return input\n",
    "\n",
    "    builtin_str = str\n",
    "    bytes = str\n",
    "    str = unicode\n",
    "\n",
    "\n",
    "elif is_py3:\n",
    "    from urllib.parse import quote as urlquote, unquote as urlunquote\n",
    "    from urllib.parse import urlparse\n",
    "\n",
    "    def to_bytes(data):\n",
    "        \"\"\"若输入为str（即unicode），则转为utf-8编码的bytes；其他则原样返回\"\"\"\n",
    "        if isinstance(data, str):\n",
    "            return data.encode(encoding='utf-8')\n",
    "        else:\n",
    "            return data\n",
    "\n",
    "    def to_string(data):\n",
    "        \"\"\"若输入为bytes，则认为是utf-8编码，并返回str\"\"\"\n",
    "        if isinstance(data, bytes):\n",
    "            return data.decode('utf-8')\n",
    "        else:\n",
    "            return data\n",
    "\n",
    "    def to_unicode(data):\n",
    "        \"\"\"把输入转换为unicode，要求输入是unicode或者utf-8编码的bytes。\"\"\"\n",
    "        return to_string(data)\n",
    "\n",
    "    def stringify(input):\n",
    "        return input\n",
    "\n",
    "    builtin_str = str\n",
    "    bytes = bytes\n",
    "    str = str"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# OS版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nt\n",
      "win32\n",
      "Windows\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import sys\n",
    "import platform\n",
    "\n",
    "print(os.name)\n",
    "print(sys.platform)\n",
    "print(platform.system())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 内置函数转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "PY3 = sys.version_info[0] == 3\n",
    "_SENTINEL = object()\n",
    "\n",
    "if PY3:\n",
    "    long = int\n",
    "    xrange = range\n",
    "    unicode = str\n",
    "    basestring = str\n",
    "    range = range\n",
    "\n",
    "    def u(s):\n",
    "        return s\n",
    "\n",
    "    def b(s):\n",
    "        return s.encode(\"latin-1\")\n",
    "else:\n",
    "    long = long\n",
    "    range = xrange\n",
    "    unicode = unicode\n",
    "    basestring = basestring\n",
    "\n",
    "    def u(s):\n",
    "        return unicode(s, \"unicode_escape\")\n",
    "\n",
    "    def b(s):\n",
    "        return s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# urllib 兼容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from six.moves.urllib.parse import urlparse"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
